package cn.stylefeng.guns.sys.util;


import org.apache.commons.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.net.URLEncoder;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSAUtil {

    //生成秘钥对
    public static KeyPair getKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        return keyPair;
    }

    //获取公钥(Base64编码)
    public static String getPublicKey(KeyPair keyPair){
        PublicKey publicKey = keyPair.getPublic();
        byte[] bytes = publicKey.getEncoded();
        return byte2Base64(bytes);
    }

    //获取私钥(Base64编码)
    public static String getPrivateKey(KeyPair keyPair){
        PrivateKey privateKey = keyPair.getPrivate();
        byte[] bytes = privateKey.getEncoded();
        return byte2Base64(bytes);
    }

    //将Base64编码后的公钥转换成PublicKey对象
    public static PublicKey string2PublicKey(String pubStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
        byte[] keyBytes = base642Byte(pubStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return publicKey;
    }

    //将Base64编码后的私钥转换成PrivateKey对象
    public static PrivateKey string2PrivateKey(String priStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
        byte[] keyBytes = base642Byte(priStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }

    //公钥加密
    public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws NoSuchPaddingException,
            NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] bytes = cipher.doFinal(content);
        return bytes;
    }

    //私钥解密
    public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] bytes = cipher.doFinal(content);
        return bytes;
    }

    //字节数组转Base64编码
    public static String byte2Base64(byte[] bytes){
        return new String(Base64.encodeBase64(bytes));
    }

    //Base64编码转字节数组
    public static byte[] base642Byte(String base64Key) {
        return Base64.decodeBase64(base64Key);
    }

//    public static String rsaSignHex(String content, String privateKey,String signType) {
//        try {
//            PKCS8EncodedKeySpec priPKCS8    = new PKCS8EncodedKeySpec( Base64.decode(privateKey) );
//            KeyFactory keyf = KeyFactory.getInstance("RSA");
//            PrivateKey priKey = keyf.generatePrivate(priPKCS8);//privateKey格式不会，这儿会crash
//            java.security.Signature signature = java.security.Signature.getInstance(signType);// "SHA-256"  "SHA1WithRSA"  SHA256withRSA
//            signature.initSign(priKey);
//            signature.update( content.getBytes());
//            byte[] signed = signature.sign();
//            return bytesToHex(signed);
//        }catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    public static void main(String args[]) throws Exception {

        String publicKeyStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoAXq3d8gH3CJBa7IMsRUVbEyZWW6a0JN1nKFGaeXTf2geo2Py3Z7/Lhtq6pY8bK9/Ux0LWkH8g09f3tWybg7nn9Xsj+x/AnucmGKG7qJQu1PqRB+NnbE9yIhxPQB0qRLDmruOLO4y0Jc6wTMbLyisEbZYtP3uPdaCZqjKYqVBNaz8u3YWdULDgt2FM1e1NaOwJXh1K2DyI9wnn+iLMk0+uCneA+8L6QlNasFwOer8htcBPYZCxIlHpJrYE54m/YPQNsJkrk4mHinDfNO+ba2ESo+n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gGxp5tSFlnR1z+ZYOFbG4PmSt7aytrtaTpOCw4XhZJQLXBLxE0TYZusrRnGpqi7JLmbahJIl1qeoartnQJQKBgDJ1TPdsSgjGzBoiyM0nH+k2zqbZCh79JBRO2vTyyrUOfOcNLBaOBrkUv+9MYOR0PQN7QBxYIVqiEfBZdJZfIapjGqyj5YCgtzSKL78NtgC8+zH6+S6kr5FttagDBdq1boGxODqBXAjt5lwb5jFnHRmrlBudmF/OBLF7tvYuH6KRAoGACdsupoQVQOub0DPsIztIkUJK8oTZM03iS8jf2wAYQ58MNlTw0x7j8wpZQ/724fgpD1/1D9MJsOmsEeIA2cyybXTnnBf3VZjcx2HGPRQqmQnKpd63giKsXdq9uH9KVpzRDFtZJjefFia95MhUf6iNNCUdVcFxQ7TL3KuxPhjrotkCgYBuwC49FkQjCRxMKXv4BRIrMDaCy+LR2uqxOxsHFjO1kUFKlzRNDx6U+3pDbMeMjCPl+7Ycd/YHBD/pPUuUDTMQ+YmEiV7ujBDHwMglsnYt7MyjY9HYEEzF7BsEP14xX56b/xNw9ZPjc/3A8mCLIyTCQWeMO15nbMQvitkVq3S67A==";

        PublicKey publicKey = RSAUtil.string2PublicKey(publicKeyStr);

        byte[] encryptUser = RSAUtil.publicEncrypt("{\"accessToken\":\"XXXX\",\"account\":\"service1\",\"password\":\"123456\",\"time\":\"1585897222\"}".getBytes(), publicKey);
        byte[] encryptPwd = RSAUtil.publicEncrypt("22s2Yu3".getBytes(), publicKey);

//        System.out.println(RSAUtil.byte2Base64(encrypt));



        String secUser = URLEncoder.encode(RSAUtil.byte2Base64(encryptUser),"GBK");
        String secPwd = URLEncoder.encode(RSAUtil.byte2Base64(encryptPwd),"GBK");

        System.out.println("加密secUser："+secUser);
        System.out.println("加密secPwd："+secPwd);
        secUser="gU41DqWbMNplefSbNZvrpxkWqFtdgR61gVLRNY6LqvVb72KfKfS+Uhgzlz8/sfKTq/kK4o2ET3B+uEYz2TP5fbCk37SjL9+d/naPGSf6DN3UOiOihe+Cy9fykInc2yZYZboPKL8uSADyYOuhkqwzMNMic64Bjn8/XcRuAKuvb/Y3O+jrP6JvSdwolGpYchvLIfJyuOjMW71L8oJ03ZfNr5/qh3nGhDdDFAfU0qiciIVosMqqNJ0oEEwbufaNcJeI+DeHRYwUdvk4Lzgf8rcZv6snpPbQl/xbvbgLiqeLmRy8+rUwr7NKKCkvZIRvpCXsRBh2hIHV/IzxLRlcLyTg+Q==";

//secUser="";
        PrivateKey privateKey = RSAUtil.string2PrivateKey(privateKeyStr);
        byte[] base642ByteUser = RSAUtil.base642Byte(secUser);
//        byte[] base642BytePwd = RSAUtil.base642Byte(secPwd);

        byte[] privateDecryptUser = RSAUtil.privateDecrypt(base642ByteUser, privateKey);
//        byte[] privateDecryptPwd = RSAUtil.privateDecrypt(base642BytePwd, privateKey);

        System.out.println("解密privateDecryptUser："+new String(privateDecryptUser));
//        System.out.println("解密privateDecryptPwd："+new String(privateDecryptPwd));

    }
}
